package service_register

import (
	"math/rand"
	"strconv"
	"time"
)

type ServiceInstance struct {
	InstanceId string
	ServiceId  string
	Host       string
	Port       int
	IsSecure   bool
	Metadata   map[string]string
}

func NewServiceInstance(serviceId string, host string, port int, secure bool,
	metadata map[string]string, instanceId string) (*ServiceInstance, error) {

	if len(instanceId) == 0 {
		instanceId = serviceId + "-" + strconv.FormatInt(time.Now().Unix(), 10) + "-" + strconv.Itoa(rand.Intn(9000)+1000)
	}

	return &ServiceInstance{
		InstanceId: instanceId,
		ServiceId:  serviceId,
		Host:       host,
		Port:       port,
		IsSecure:   secure,
		Metadata:   metadata,
	}, nil
}
